package com.java.wx.controller;

import com.java.bean.Courier;
import com.java.bean.Express;
import com.java.bean.Message;
import com.java.bean.User;
import com.java.mvc.ResponseBody;
import com.java.service.CourierService;
import com.java.service.ExpressService;
import com.java.service.UserService;
import com.java.util.JSONUtil;
import com.java.util.RandomUtil;
import com.java.util.SMSUtil;
import com.java.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserController {

    @ResponseBody("/wx/loginSms.do")
    public String sendSms(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
//        String code = RandomUtil.getCode()+"";
//        boolean flag = SMSUtil.loginSMS(userPhone,code);
        //测试阶段，短信已发送
        boolean flag = true;
        String code = "123456";

        Message msg = new Message();
        if(flag){
            //短信发送成功
            msg.setStatus(0);
            msg.setResult("验证码已发送，请查收！");
        }else {
            //短信发送失败
            msg.setStatus(1);
            msg.setResult("验证码发送失败，请检查手机号！");
        }
        UserUtil.setLoginSms(request.getSession(),userPhone,code);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String courierPhone = request.getParameter("courierPhone");
        String userCode = request.getParameter("code");
        String sysCode = UserUtil.getLoginSms(request.getSession(),userPhone);
        Message msg = new Message();
        if(sysCode == null){
            //手机没有获取短信验证码
            msg.setStatus(-1);
            msg.setResult("请先获取短信验证码！！");
        }else if(sysCode.equals(userCode)){
            //获取成功
            User user1 = UserService.findByUserPhone(userPhone);
            Courier courier1 = CourierService.findByPhone(courierPhone);
            if(user1 == null && courier1 ==null){
                //未存储，默认注册新用户
                msg.setStatus(0);
                User u = new User();
                u.setUserPhone(userPhone);
                UserService.insert(u);
                UserUtil.setWxUser(request.getSession(),u);
            }else if(user1 == null && courier1 !=null){
                //快递员
                msg.setStatus(1);
                UserUtil.setWxCourier(request.getSession(),courier1);
            }else if(user1 != null && courier1 ==null){
                //普通用户
                msg.setStatus(0);
                UserUtil.setWxUser(request.getSession(),user1);
            }
        }else {
            //验证码不一致
            msg.setStatus(-2);
            msg.setResult("验证码不一致，请检查！");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/userInfo.do")
    public String userInfo(HttpServletRequest request, HttpServletResponse response){
        User user = UserUtil.getWxUser(request.getSession());
        boolean isUser = user.isUser();
        Message msg = new Message();
        if(isUser){
            msg.setStatus(0);
        }else {
            msg.setStatus(1);
        }
        msg.setResult(user.getUserPhone());
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response){
        request.getSession().invalidate();
        Message msg = new Message();
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/express/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String company = request.getParameter("company");
        Express e = new Express();
        e.setNumber(number);
        e.setUsername(username);
        e.setUserPhone(userPhone);
        e.setCompany(company);
        e.setSysPhone(UserUtil.getUserPhone(request.getSession()));
        boolean flag = ExpressService.insert(e);
        Message msg = new Message();
//        if(e == null)
        if(flag){
            msg.setStatus(0);
            msg.setResult("快递录入成功！");
        }else {
            msg.setStatus(-1);
            msg.setResult("快递录入失败！");
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }
}
